package collection;

import com.sun.security.auth.UnixNumericUserPrincipal;

import java.util.Iterator;

public class MyLinkedList implements Iterable<Integer> {
    private Node head;
    private Node tail;
    private int size;

    public void add(int value){
        if(size == 0){//当没有节点时，也可判断head==null;
            head = new Node(value);
            tail = head;
            return;
        }
        Node newNode = new Node(value);
        tail.next = newNode;
        newNode.prev = tail;
        size++;
        tail = newNode;
    }

    public int get(int index){
         Node n = getNode(index);
         return n.value;
    }

    private Node getNode(int index) {
        if(index < size/2){
            Node n = head;
            int count = 0;
            while (count< index){
                n = n.next;
                count++;
            }
            return n;
        }else {
            Node n = tail;
            int count = size-1;
            while (count> index){
                n = n.prev;
                count--;
            }
            return n;
        }
    }

    public int getFirst(){
        if(head == null ){
            throw new RuntimeException("没有数据异常");
        }
        return head.value;
    }
    public int getLast(){
        if(size== 0){
            throw new RuntimeException("没有数据异常");
        }
        return tail.value;
    }
    public int size(){
        return size;
    }
    public void remove(int index){
        if (size == 0) {
            throw new IndexOutOfBoundsException("下标越界");
        }
        if (index == 0) {
            head = head.next;   //把头部节点的下一个，设置成新的头部节点
            head.prev = null;   //新的头部节点的前一个，设置成null
            size--;             //数据量减1
            return;
        } else if (index == size-1) {
            tail = tail.prev;
            tail.next = null;
            size--;
            return;
        }

        Node n = getNode(index);

        n.prev.next = n.next;  //n前一个节点的下一个，引用n的下一个节点
        n.next.prev = n.prev;  //n下一个节点的前一个，应用n的前一个节点

        size--;

    }
    public Iterator<Integer> iterator(){
        return new Iterator<Integer>() {
            Node n = head;

            @Override
            public boolean hasNext() {
                return n!= null;
            }

            @Override
            public Integer next() {
                Node tmp = n;
                n = n.next;
                return tmp.value;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("不支持删除操作");
            }
        };
    }



    static class Node{
        private int value;
        private Node prev;
        private Node next;
        Node(int value){
            this.value = value;
        }
    }

    public static void main(String[] args) {
        MyArrayList list = new MyArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        System.out.println(list.size());
        System.out.println(list.get(3));
        System.out.println(list.get(4));

        System.out.println("------------------");
        Iterator<Integer> it = list.iterator();
        while (it.hasNext()){
            Integer v = it.next();
            System.out.println(v);
        }
        System.out.println("++++++++++++++");
        for (Integer v:list
             ) {
            System.out.println(v);
        }
    }
}
